package cc.eddic.examinationsystem.web;


import cc.eddic.examinationsystem.core.BaseController;
import cc.eddic.examinationsystem.domain.ExamQuestion;
import cc.eddic.examinationsystem.domain.Option;
import cc.eddic.examinationsystem.domain.Question;

import cc.eddic.examinationsystem.repository.ExamQuestionRepository;
import cc.eddic.examinationsystem.repository.ExamRepository;
import cc.eddic.examinationsystem.repository.QuestionRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Controller
@RequestMapping("/wqj")
public class QuestionSelectController  extends BaseController {

    private final QuestionRepository questionService; // Inject your QuestionService here

    // Constructor injection
    @Autowired
    public QuestionSelectController(QuestionRepository questionService) {
        this.questionService = questionService;
    }

    @Autowired
    private ExamQuestionRepository examQuestionRepository;

    @Autowired
    private QuestionRepository questionRepository;

    @Autowired
    private ExamRepository examRepository;

    @GetMapping("/exam_select")
    public String examSelect(Model model){
        model.addAttribute("exams",examRepository.findAll());
        return "exam-select-wqj";
    }

    @GetMapping("/exam_questions")
    public String examQuestions(Model model) {
        examQuestionRepository.findAll().size();
        List<Integer> exam_nums = new ArrayList<>();
        exam_nums.add(1);
        exam_nums.add(2);

        List<ExamQuestion> examQuestions = examQuestionRepository.findAll();
        List<Question> questions=examQuestions.stream().map(ExamQuestion::getQuestion).collect(Collectors.toList());

        model.addAttribute("exam_nums", exam_nums);
        model.addAttribute("questionList", questions);

        return "preview";
    }
}

